package cn.peach.hrm.controller;

import cn.peach.hrm.dto.CourseAddParamDto;
import cn.peach.hrm.query.CourseQuery;
import cn.peach.hrm.service.ICourseService;
import cn.peach.hrm.domain.Course;
import cn.peach.hrm.util.AjaxResult;
import cn.peach.hrm.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/course")
public class
CourseController {
    @Autowired
    public ICourseService courseService;

        /**
         * 课程上线
         */
        @RequestMapping(value = "/onLineCourse/{id}", method = RequestMethod.POST)
        public AjaxResult onLineCourse (@PathVariable("id") Long id){
            courseService.onLineCourse(id);
            return AjaxResult.me();
        }

        /**
         * 保存
         */
        @RequestMapping(value = "/save", method = RequestMethod.POST)
        public AjaxResult save (@RequestBody @Valid CourseAddParamDto dto){
            courseService.save(dto);
            return AjaxResult.me();
        }

        /**
         * 删除对象
         */
        @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
        public AjaxResult delete (@PathVariable("id") Long id){
            courseService.deleteById(id);
            return AjaxResult.me();
        }

        /**
         * 根据ID获取对象
         */
        @RequestMapping(value = "/{id}", method = RequestMethod.GET)
        public AjaxResult get (@PathVariable("id") Long id){
            return AjaxResult.me().setResultObj(courseService.selectById(id));
        }


        /**
         * 获取所有对象
         */
        @RequestMapping(value = "/list", method = RequestMethod.GET)
        public AjaxResult list () {
            return AjaxResult.me().setResultObj(courseService.selectList(null));
        }

        /**
         * 分页查询数据
         */
        @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
        public AjaxResult json (@RequestBody CourseQuery query){
            Page<Course> page = new Page<Course>(query.getPage(), query.getRows());
            page = courseService.selectPage(page);
            PageList pageList = new PageList<Course>(page.getTotal(), page.getRecords());
            return AjaxResult.me().setResultObj(pageList);
        }
    }